<?php

require_once APPPATH."/controllers/BasePriv.php";
require_once APPPATH."/libraries/Tools.php";
require APPPATH."/libraries/FFmpeg.php";
use Aws\S3\S3Client;
class Music extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
        $this->load->model("Host_music_model", "table");
        $this->music_type="music_type";//音效分类表
        $this->music="music";//音效表
    }
////////////////////////////////////////////////////////////////////////音效分类开始/////////////////////////////////////////////////////////////////////////////

    /**
     * 音效分类主页
     */
    public function index(){
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 50;
        $res=$this->table->all($arr);
        if(!empty($res['row'])){
            foreach($res['row'] as $k=>&$v){
                $res1=$this->table->all_nums($v['id']);
                if(empty($res1)){
                    $v['nums']=0;
                }else{
                    $v['nums']=count($res1);
                }
            }
        }
        $data['data']=$res['row'];
        $this->layout("no_edit/music/index",$data);
    }

    /**
     * 新增音效分类
     */
    public function add_music(){
        $this->layout("no_edit/music/add_music");
    }

    /**
     * 保存新增音效分类
     */
    public function save_music(){
        if(empty($_POST['music_type_name'])){
            response(-1,"音效名称不能为空");
        }
        if(empty($_POST['music_type_icon'])){
            response(-1,"音效选中图标不能为空");
        }
        if(empty($_POST['music_no_type_icon'])){
            response(-1,"音效选中图标不能为空");
        }
        if(mb_strlen($_POST['music_type_name'])>15){
            response(-1,"最大字数为15字");
        }
        $data['test_music_type_name']=$_POST['music_type_name'];
        $data['test_music_type_icon']=$_POST['music_type_icon'];
        $data['test_no_music_type_icon']=$_POST['music_no_type_icon'];
        $data['add_time']=date("Y-m-d H:i:s",time());
        $data['update_time']=date("Y-m-d H:i:s",time());
        $data['music_tmp']=$this->getMsecTime();
//        var_dump($data);exit;
        $res=$this->table->add($this->music_type,$data);
        if($res){
            response(0,"添加成功",base_url("no_edit/music/index"));
        }else{
            response(-1,"添加失败");
        }
    }

    /**
     * 分类启用
     */
    public function start_type(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $res=$this->table->update($this->music_type,['test_music_type_status'=>2],['id'=>$id]);
        if($res){
            response(0,"启用成功");
        }else{
            response(-1,"启用失败");
        }
    }

    /**
     * 分类停用
     */
    public function stop_type(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $res=$this->table->update($this->music_type,['test_music_type_status'=>1],['id'=>$id]);
        if($res){
            response(0,"停用成功");
        }else{
            response(-1,"停用失败");
        }
    }

    /**
     * 分类上移
     */
    public function up_type(){
        $id=$_POST['id'];
        $start_id=$_POST['start_id'];

        $res=$this->table->up_type($id,$start_id);
        if($res){
            response(0,"上移成功");
        }else{
            response(-1,"上移失败");
        }
    }

    /**
     * 分类下移
     */
    public function down_type(){
        $id=$_POST['id'];
        $end_id=$_POST['end_id'];

        $res=$this->table->down_type($id,$end_id);
        if($res){
            response(0,"下移成功");
        }else{
            response(-1,"下移失败");
        }
    }

    /**
     * 音效分类编辑
     */
    public function edit(){
        //查询这个音效分类的信息
        $id=$_GET['id'];
        $music_type=$this->table->one($this->music_type,['id'=>$id]);
        $data['music_type']=$music_type;
        //查询这个音效分类下的音效
        $music=$this->table->music_all($id);
        if(!empty($music)){
            $ffmpeg = new FFmpeg(false);

            foreach($music as $k=>&$v){
                $v['music_time']=$ffmpeg->formatFFmpegTime($v['music_time']);
            }
        }
        $data['music']=$music;
        $this->layout("no_edit/music/edit",$data);
    }

    /**
     * 保存音效分类编辑
     */
    public function save_edit(){
        if(empty($_POST['music_type_name'])){
            response(-1,"音效名称不能为空");
        }
        if(empty($_POST['music_type_icon'])){
            response(-1,"音效选中图标不能为空");
        }
        if(empty($_POST['music_no_type_icon'])){
            response(-1,"音效未选中图标不能为空");
        }
        if(mb_strlen($_POST['music_type_name'])>15){
            response(-1,"最大字数为15字");
        }
        $data['test_music_type_name']=$_POST['music_type_name'];
        $data['test_music_type_icon']=$_POST['music_type_icon'];
        $data['test_no_music_type_icon']=$_POST['music_no_type_icon'];
        $data['update_time']=date("Y-m-d H:i:s",time());
//        var_dump($data);exit;
        $res=$this->table->update($this->music_type,$data,['id'=>$_POST['id']]);
        if($res){
            response(0,"保存成功",base_url("no_edit/music/index"));
        }else{
            response(-1,"保存失败");
        }
    }

////////////////////////////////////////////////////////////////////////音效分类结束///////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////音效开始///////////////////////////////////////////////////////////////////////////////////

    /**
     * 添加音效
     */
    public function music_add(){
        if(empty($_POST['music_name'])){
            response(-1,"请添加音效名称");
        }
        if(empty($_POST['music_url'])){
            response(-1,"请添加音效地址");
        }
        if(empty($_POST['music_type_id'])){
            response(-1,"缺少参数");
        }
        //获取音效时长
        $ffmpeg = new FFmpeg(false);

        $time1=$ffmpeg->getVideoLen($_POST['music_url']);
        $data['music_time']=sprintf("%.2f",$time1);
//        $data['music_time']=$ffmpeg->formatFFmpegTime($time2);

        $data['music_type_id']=$_POST['music_type_id'];
        $data['music_name']=$_POST['music_name'];
        $data['music_url']=$_POST['music_url'];
        $data['music_time_tmp']=$this->getMsecTime();
        $data['add_time']=date("Y-m-d H:i:s",time());
        $data['update_time']=date("Y-m-d H:i:s",time());
//        var_dump($data);exit;
        $res=$this->table->add($this->music,$data);
        if($res){
            response(0,"添加成功");
        }else{
            response(-1,"添加失败");
        }
    }

    /**
     * 修改音效
     */
    public function save_edit_music(){
        if(empty($_POST['music_name'])){
            response(-1,"请添加音效名称");
        }
        if(empty($_POST['music_url'])){
            response(-1,"请添加音效地址");
        }
        if(empty($_POST['music_id'])){
            response(-1,"缺少参数");
        }
        //判断音频是否更改过
        if($_POST['music_url'] != $_POST['old_music_url']){
            $data['music_url']=$_POST['music_url'];
            //获取音效时长
            $ffmpeg = new FFmpeg(false);
            $time1=$ffmpeg->getVideoLen($_POST['music_url']);
            $data['music_time']=sprintf("%.2f",$time1);
        }else{
            $data['music_url']=$_POST['music_url'];
        }
        $data['music_name']=$_POST['music_name'];
        $res=$this->table->update($this->music,$data,['id'=>$_POST['music_id']]);
        if($res){
            response(0,"修改成功");
        }else{
            response(-1,"修改失败");
        }
    }

    /**
     * 音效启用
     */
    public function start_music(){
        if(empty($_POST['music_id'])){
            response(-1,"缺少参数");
        }
        $res=$this->table->update($this->music,['music_status'=>2],['id'=>$_POST['music_id']]);
        if($res){
            response(0,"启用成功");
        }else{
            response(-1,"启用失败");
        }
    }

    /**
     * 音效停用
     */
    public function stop_music(){
        if(empty($_POST['music_id'])){
            response(-1,"缺少参数");
        }
        $res=$this->table->update($this->music,['music_status'=>1],['id'=>$_POST['music_id']]);
        if($res){
            response(0,"停用成功");
        }else{
            response(-1,"停用失败");
        }
    }

    /**
     * 音效上移
     */
    public function up_music(){
        $id=$_POST['music_id'];
        $start_id=$_POST['start_id'];
//var_dump($_POST);exit;
        $res=$this->table->up_music($id,$start_id);
        if($res){
            response(0,"上移成功");
        }else{
            response(-1,"上移失败");
        }
    }

    /**
     * 音效下移
     */
    public function down_music(){
        $id=$_POST['music_id'];
        $end_id=$_POST['end_id'];

        $res=$this->table->down_music($id,$end_id);
        if($res){
            response(0,"下移成功");
        }else{
            response(-1,"下移失败");
        }
    }

/////////////////////////////////////////////////////////////////////////音效结束///////////////////////////////////////////////////////////////////////////////////

    /**
     * 发布
     */
    public function fabu(){
        $res=$this->table->fabu();
        if($res){
            response(0,"发布成功");
        }else{
            response(-1,"发布失败");
        }
    }

    /**
     * 获取毫秒级别的时间戳
     */
    public function getMsecTime()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }

}